package gui;

import javax.swing.*;

import shape.Circle;
import shape.Line;
import shape.Rectangle;
import engine.SvgObjects;
import java.awt.*;
import java.awt.geom.*;
import java.util.ArrayList;

/**
 * Paints objects from svgobjects into JPanel
 */

@SuppressWarnings("serial")
public class DrawShapes extends JPanel
{
    private ArrayList<Rectangle> rectangles;
    private ArrayList<Circle>    circles;
    private ArrayList<Line>      lines;
    
    /***
     ** constructor
     * 
     * @param ob
     *            is the svgobject to be loaded
     */
    public DrawShapes(SvgObjects ob)
    {
        this.rectangles = ob.getRectangles();
        this.circles = ob.getCircles();
        this.lines = ob.getLines();
    }
    
    /**
     * Overload constructor
     */
    public DrawShapes()
    {
        rectangles = null;
        circles = null;
        lines = null;
    }
    
    /**
     * This method paints shapes onto the JPanel The shapes are pulled from
     * three array lists
     * 
     * @param g
     */
    protected void paintComponent(Graphics g)
    {
        if (rectangles == null || circles == null || lines == null) return;
        Graphics2D g2d = (Graphics2D) g;
        
        /* paint all rectangles */
        for (int i = 0; i < rectangles.size(); i++)
        {
            g2d.setColor(rectangles.get(i).getStroke());
            g2d.setStroke(new BasicStroke((int) rectangles.get(i)
                    .getStrokeWidth()));
            g2d.drawRect((int) rectangles.get(i).getX(), (int) rectangles
                    .get(i).getY(), (int) rectangles.get(i).getWidth(),
                    (int) rectangles.get(i).getHeight());
            g2d.setPaint(rectangles.get(i).getFillColor());
            g2d.fillRect((int) rectangles.get(i).getX(), (int) rectangles
                    .get(i).getY(), (int) rectangles.get(i).getWidth(),
                    (int) rectangles.get(i).getHeight());
        }
        /* paint all circles */
        for (int i = 0; i < circles.size(); i++)
        {
            Shape circle = new Ellipse2D.Double(circles.get(i).getCx(), circles
                    .get(i).getCy(), circles.get(i).getRadius(), circles.get(i)
                    .getRadius());
            g2d.setColor(circles.get(i).getStroke());
            g2d.setStroke(new BasicStroke((int) circles.get(i).getStrokeWidth()));
            g2d.draw(circle);
            g2d.setPaint(circles.get(i).getFillColor());
            g2d.fill(circle);
        }
        /* paint all lines */
        for (int i = 0; i < lines.size(); i++)
        {
            g2d.setColor(lines.get(i).getStrokeColor());
            g2d.setStroke(new BasicStroke((int) lines.get(i).getStrokeWidth()));
            g2d.drawLine((int) lines.get(i).getX1(),
                    (int) lines.get(i).getY1(), (int) lines.get(i).getX2(),
                    (int) lines.get(i).getY2());
        }
        
    }
    
    /**
     * Refresh method This method forces the JPanel to be repainted
     */
    public void reload()
    {
        repaint();
    }
    
    /**
     * This method adds a new SvgObject onto the JPanel
     * 
     * @param ob
     */
    public void addOB(SvgObjects ob)
    {
        this.rectangles = ob.getRectangles();
        this.circles = ob.getCircles();
        this.lines = ob.getLines();
        repaint();
    }
}
